﻿using AutoMapper;
using CSRedis;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using WebApplication1.API返回类;
using WebApplication1.DTO.测试;
using WebApplication1.Models.RBAC;

namespace WebApplication1.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class StudentController : ControllerBase
    {
        private readonly ISqlSugarClient _db;
        private readonly IMapper mapper;

        public StudentController(SqlSugar.ISqlSugarClient db,IMapper mapper)
        {
            this._db = db;
            this.mapper = mapper;
        }

        /// <summary>
        /// 查询学生信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetStudentAll(int pageindex,int pagesize)
        {
            var list = _db.Queryable<StudentModel>().ToPageList(pageindex, pagesize).ToList();// 不返回Count;
            return Ok(list);
        }

        /// <summary>
        /// 添加学生信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult CreateStudent(StudentModel s)
        {
            var r= _db.Insertable(s).ExecuteReturnIdentity();
            return Ok(r);
        }

        /// <summary>
        /// 删除学生信息
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        public IActionResult DeleteStudent(int id)
        {
            //单个实体
            var r = _db.Deleteable<StudentModel>(new StudentModel() { Id = id }).ExecuteCommand();

            return Ok(r);
        }

        /// <summary>
        /// 修改学生信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdateStudent(StudentModel s)
        {
            //根据主键更新单条 参数 Class
            var r = _db.Updateable(s).ExecuteCommand();//实体有多少列更新多少列

            return Ok(r);
        }

        /// <summary>
        /// 上传
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Upload(IFormFile file)
        {
            var path = Directory.GetCurrentDirectory() + @"/wwwroot/pic/" + file.FileName;
            using (FileStream fs = new FileStream(path,FileMode.Create))
            {
                file.CopyTo(fs);
                fs.Close();
            }

            return Ok("http://localhost:5028/pic/" + file.FileName);
        }

        [HttpGet]
        public IActionResult Main1()
        {
            var redis = new CSRedis.CSRedisClient("127.0.0.1:6379,password=123,defaultDatabase=13,prefix=key前辍");
            //var redis = new RedisClient("localhost:127.0.0.1:6379");
            redis.Set("myKey","myValue");

            string value = redis.Get("myKey");
            Console.WriteLine(value);
            return Ok("完成");
        }

        [HttpPost]
        public IActionResult Main2(SiteDto dto)
        {
            SiteDto model = mapper.Map<SiteDto>(dto);
            return Ok("完成");
            
        }
        
    }

    public class Method()
    {
        //无返回值委托
        public delegate void MyDelegate();

        //有返回值委托
        public delegate int MyDelegate1(int a, int b);

        public delegate string MyDelegate2(string s);

        public void MethodA()
        {
            Console.WriteLine("方法A被调用");
        }

        public void MethodB()
        {
            Console.WriteLine("方法B被调用");
        }

        public int MethodC(int a, int b)
        {
            return a + b;
        }

        /// <summary>
        /// 无返回值方法
        /// </summary>
        public void Main()
        {
            //内置委托(微软官方定义好的委托,可以拿来直接用) Action Func
            //有参数,无返回值 Action
            Action<int, string, bool> action = (a, b, c) => { };
            Action action1 = () => { };//无参数无返回值
            //有参数,有返回值 Func
            Func<int> func = () => { return 1; };

            Func<int, string> func1 = (int a) => { return a.ToString(); };


            //定义委托(委托类型-- 引用类型)
            MyDelegate myDelegate = MethodA;

            myDelegate();

            MyDelegate myDelegate11 = delegate () { Console.WriteLine("MedthodA被调用"); };

            //委托指定一个匿名方法
            MyDelegate myDelegateTest = () =>
            {
                Console.WriteLine("MedthodA被调用");
            };
            MyDelegate myDel = () => { };

            MyDelegate1 myDelegate1 = (int a, int b) => { return a + b; };

            MyDelegate2 myDelegate2 = (string s) => { return s; };


        }
    }
}

